package com.dongdongsongcan.app.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.activity.LoginActivity;
import com.dongdongsongcan.app.activity.ShopDetailActivity;
import com.dongdongsongcan.app.activity.SubmitOrderActivity;
import com.dongdongsongcan.app.adapter.FoodlistAdapter;
import com.dongdongsongcan.app.adapter.ShopCarAdapter;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.ShopControl;
import com.dongdongsongcan.app.control.ShoppingCarControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.MenuGroupData;
import com.dongdongsongcan.app.control.bean.MenuGroupData.MenuGroup;
import com.dongdongsongcan.app.control.bean.MenuListData;
import com.dongdongsongcan.app.control.bean.MenuListData.MenuList;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.db.ShoppingCarDao;
import com.dongdongsongcan.app.db.bean.ShoppingCarDBBean;
import com.dongdongsongcan.app.view.BadgeView;
import com.su.dolphin.adapter.DpBaseAdapter;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.ViewUtil;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.fragment.BaseFragment;

import java.util.List;

public class ShopDetailFoodlistFragment extends BaseFragment implements DpMessageCenter.INotify {

    private ListView mShopdetail_category, mShopdetail_foods;
    private FoodlistAdapter mFoodlistAdapter;
    private CategoryAdapter mCategoryAdapter;
    private ShopData.Shop mShop;
    private TextView mVf_shoppingcar_toolbar_desc;
    private TextView mVf_shoppingcar_toolbar_ok;
    private View mShopcar_button;
    private ListView mShoppingcar_list;
    private ShopCarAdapter mShopCarAdapter;
    private View mShoppingcar_layer;
    private BadgeView mBadgeView;
    private View shoppingcar_clear;

    @Override
    protected int onSetContentView() {
        DpMessageCenter.i().register(this, DDMessageDef.N_SUBMIT_ORDER_SUCCESS);
        mShop = ((ShopDetailActivity) getActivity()).getShop();
        return R.layout.fragment_shopdetail_list;
    }

    @Override
    public void initView() {
        shoppingcar_clear = getView().findViewById(R.id.shoppingcar_clear);
        mShoppingcar_layer = getView().findViewById(R.id.shoppingcar_layer);
        mShoppingcar_list = (ListView) getView().findViewById(R.id.shoppingcar_list);
        mShopcar_button = getView().findViewById(R.id.shopcar_button);
        mVf_shoppingcar_toolbar_desc = (TextView) getView().findViewById(
                R.id.vf_shoppingcar_toolbar_desc);
        mVf_shoppingcar_toolbar_ok = (TextView) getView().findViewById(
                R.id.vf_shoppingcar_toolbar_ok);

        mShopdetail_category = (ListView) getView().findViewById(
                R.id.shopdetail_category);
        mShopdetail_foods = (ListView) getView().findViewById(
                R.id.shopdetail_foods);
        mFoodlistAdapter = new FoodlistAdapter(getActivity(), this);
        mCategoryAdapter = new CategoryAdapter(getActivity());
        mShopdetail_category.setAdapter(mCategoryAdapter);
        mShopdetail_foods.setAdapter(mFoodlistAdapter);
        mShopdetail_category.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        FrameLayout shopcar_icon = (FrameLayout) getView().findViewById(R.id.shopcar_icon);
        mBadgeView = new BadgeView(getActivity());
        mBadgeView.setTargetView(shopcar_icon);

    }

    @Override
    public void setListener() {
        shoppingcar_clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TipsUtil.showConfirmDialog(getActivity(), "请确认", "确定要删除购物车吗?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ShoppingCarDao.clearShopingCar(mShop.getShop_id());
                        reloadShopCar();
                    }
                });
            }
        });
        mShoppingcar_layer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mShoppingcar_layer.setVisibility(View.INVISIBLE);
                return false;
            }
        });
        mShopcar_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showShopCarList();
            }


        });
        mVf_shoppingcar_toolbar_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!UserControl.i().userExist()) {
                    LoginActivity.startFrom(getActivity());
                    return;
                }
                List<ShoppingCarDBBean> shoppingCarDBBeans = ShoppingCarControl.i().getShoppingCarList(mShop.getShop_id());
                if (shoppingCarDBBeans == null || shoppingCarDBBeans.isEmpty()) {
                    TipsUtil.toast("购物车为空!");
                    return;
                }
                SubmitOrderActivity.startFrom(getActivity(), mShop);
            }
        });
    }

    @Override
    public void initData() {
        mShopCarAdapter = new ShopCarAdapter(getActivity(), this);
        mShoppingcar_list.setAdapter(mShopCarAdapter);
        // ShoppingCarControl.i().setShoppingCarToolbar(mShop.getShop_id(), mVf_shoppingcar_toolbar_desc, mVf_shoppingcar_toolbar_ok, mBadgeView);
        ShopControl.i().getMenuGroupData(new IControlResponse<MenuGroupData>() {

            @Override
            public void onControlResponse(MenuGroupData data) {
                mCategoryAdapter.setData(data.getResult());
                mShopdetail_category.setItemChecked(0, true);
                if (data.getResult().size() > 1) {
                    getMenuListData(data.getResult().get(0).getGroup_id());
                }
            }
        }, mShop.getShop_id());
    }

    @Override
    public void onClick(View arg0) {

    }

    @Override
    public void update(Bundle bundle) {

    }


    private void showShopCarList() {

        if (mShoppingcar_layer.getVisibility() != View.VISIBLE) {
            List<ShoppingCarDBBean> list = ShoppingCarControl.i().getShoppingCarList(mShop.getShop_id());
            if (list != null && !list.isEmpty()) {
                mShopCarAdapter.setData(list);
                mShoppingcar_layer.setVisibility(View.VISIBLE);
            }
        } else {
            mShoppingcar_layer.setVisibility(View.INVISIBLE);
        }

    }


    private void getMenuListData(int groupid) {
        ShopControl.i().getMenuListData(new IControlResponse<MenuListData>() {

            @Override
            public void onControlResponse(MenuListData data) {
                mFoodlistAdapter.setData(data.getResult());
            }
        }, mShop.getShop_id(), groupid, 0, 0);

//        List<MenuList> list = new ArrayList<MenuList>();
//        MenuList menuList = new MenuList();
//        menuList.setMenu_title("Test");
//        menuList.setMenu_id(223);
//        menuList.setMenu_pic_small("xxx");
//        list.add(menuList);
//        menuList = new MenuList();
//        menuList.setMenu_title("Test");
//        menuList.setMenu_id(224);
//        menuList.setMenu_pic_small("xxx");
//        list.add(menuList);
        //mFoodlistAdapter.setData(list);
    }

    @Override
    public void onNotifyInner(DpMessageCenter.MCMessage mcmessage) {
        if (mcmessage.notificationID == DDMessageDef.N_SUBMIT_ORDER_SUCCESS) {
            mFoodlistAdapter.notifyDataSetChanged();
        }
    }

    public boolean onBackPressed() {
        if (mShoppingcar_layer.getVisibility() == View.VISIBLE) {
            mShoppingcar_layer.setVisibility(View.GONE);
            return true;
        }
        return false;
    }

    class CategoryAdapter extends DpBaseAdapter<MenuGroup> {

        public CategoryAdapter(Context context) {
            super(context);
        }

        @Override
        public int onGetItemLayout() {
            return R.layout.layout_shopdetail_menugroup_item;
        }

        @Override
        public void onGetView(View convertView, final int position) {
            MenuGroup menuGroup = mDataList.get(position);
            TextView shopdetail_item_name = findViewById(convertView,
                    R.id.shopdetail_item_name);
            shopdetail_item_name.setText(menuGroup.getGroup_name());
            shopdetail_item_name.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mShopdetail_category.setItemChecked(position, true);
                    int groupid = mDataList.get(position).getGroup_id();
                    getMenuListData(groupid);
                }

            });
            if (mShopdetail_category.isItemChecked(position)) {
                shopdetail_item_name.setBackgroundColor(0xFF00C6AF);
                shopdetail_item_name.setTextColor(Color.WHITE);
            } else {
                shopdetail_item_name.setBackgroundColor(Color.WHITE);
                shopdetail_item_name.setTextColor(0xFF555555);
            }

        }


    }

    public ShopData.Shop getShop() {
        return mShop;
    }

    public void deleteProductFromShoppigCar(MenuList menuList) {
        ShoppingCarControl.i().delete(menuList.getMenu_id());
        refreshShopCar();
    }

    public void addToShoppingCar(MenuList menuList, String make, String weight) {
        ShoppingCarDBBean shoppingCarDBBean = new ShoppingCarDBBean(mShop.getShop_id(),
                menuList.getMenu_id(), menuList.getMenu_title(), 1,
                menuList.getMenu_price(), menuList.getMenu_bag_price(), menuList.getMenu_pic_small(), make
                , weight);
        ShoppingCarControl.i().save(shoppingCarDBBean);
        refreshShopCar();
    }

    public void reloadShopCar() {
        List<ShoppingCarDBBean> list = ShoppingCarControl.i().getShoppingCarList(mShop.getShop_id());
        if (list != null && !list.isEmpty()) {
            mShopCarAdapter.setData(list);
        } else {
            mShoppingcar_layer.setVisibility(View.INVISIBLE);
        }
        refreshShopCar();
    }

    public void refreshShopCar() {
        mFoodlistAdapter.notifyDataSetChanged();
        ShoppingCarControl.i().setShoppingCarToolbar(mShop.getShop_id(), mVf_shoppingcar_toolbar_desc, mVf_shoppingcar_toolbar_ok, mBadgeView);
    }

    @Override
    public void onResume() {
        refreshShopCar();
        super.onResume();
    }
}
